#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include "dht.h"

//initialize the variables
struct sockaddr_in this_client_info;
struct sockaddr_in dht_node_info;
int root_node_port;
int this_client_socket;
int bytes_recieved;
int this_node_port_num;

char root_node_ip[64];
char send_data[1024],recv_data[1024];


int main(int argc, char *argv[]){
  	if (argc !=4) {
	  
	  printf("Usage: \n %s <dhtnode> <id> <entry>\n", argv[0]);
	  
	} else {
	  //creating node_input and pointer node_in   
	  char node_input[64];
	  char *node_in;
	  
	  strncpy(node_input, argv[1], 64-1);
		
	  //char *node_input;
	  node_in = strtok(node_input, ":");	  
	  if(node_in)
	    {//takes in the root node ip
	      strncpy(root_node_ip, node_in, 64-1);
	      printf("Root Node IP\t: %s\n", root_node_ip);
	    }
	  node_in = strtok(NULL, ":");
	  
	  if(node_in) {//takes in the portn number
	    root_node_port = atoi(node_in);
	    printf("Root Node Port\t: %i\n", root_node_port);
	  }
	  
	  //creating dir_id to hold the email address
	  char dir_id[64];
	  strncpy(dir_id, argv[2], 64-1);
	  printf("ID is: %s\n", dir_id);
	  
	  //creating entry to hold the entry to be sent
	  char entry[512];
	  strncpy(entry, argv[3], 512-1);
	  printf("Entry is: %s\n", entry);
	  
	  //getting the size of email and message
	  size_t len1 = strlen(dir_id);
	  size_t len2 = strlen(entry);
        
	  char * s = malloc(len1 + len2 + 4);//allocating memory space
	  //putting things into the memory space
	  memcpy(s, "1|", 2);
	  memcpy(s + 2, dir_id, len1);
	  s[len1+2] = '|';
	  memcpy(s + len1 + 3, entry, len2 + 1); 
	  
	  //sending...
	  node_connect(root_node_ip,root_node_port, s);
	}
	
}


int node_connect(int dst_ip[64], int dst_port, char send_data[512]){
  //to send things over the the node, taking in arguments, the node's IP, port and the data to be sent
  //creating socket using TCP
  if ((this_client_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
    perror("Socket Error");
    exit(EXIT_FAILURE);
  }
  
  //setting up the connection...  
  this_client_info.sin_family = AF_INET; 
  inet_aton(dst_ip, &(this_client_info.sin_addr)); 
  this_client_info.sin_port = htons(dst_port);
  bzero(&(this_client_info.sin_zero), 8);
  printf("connected...\n");
  
  if (connect(this_client_socket, (struct sockaddr *)&this_client_info,
	      sizeof(struct sockaddr)) == -1)
    {//if connection fail, display error
      perror("Connect Error\t");
      exit(1);
    }
  //connection is a success, send data
  send(this_client_socket,send_data,strlen(send_data), 0);   
  
  //closing the connection
  close(this_client_socket);

    /*while(1)
    {
        
       bytes_recieved=recv(this_client_socket,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        
        if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
        {
            close(this_client_socket);
            break;
        }
        
        else
            printf("\nRecieved data = %s " , recv_data);
        
        printf("\nSEND (q or Q to quit) : ");
        fflush(stdout);
        //gets(send_data);
        
        if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
            send(this_client_socket,send_data,strlen(send_data), 0); 
        
        else
        {
            send(this_client_socket,send_data,strlen(send_data), 0);   
            close(this_client_socket);
            break;
        }
        
    }  */ 
    return 0;



	
}



